﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Threading;

namespace WF.Girl
{
    public partial class GForm : Form
    {
        public GForm()
        {
            InitializeComponent();
        }

        private A.HTMlServer.DCommon dc = new A.HTMlServer.DCommon();
        private object objState = new object();
        private int GCount;
        delegate void dfun();

        A.HTMlServer.HTML.GetOnePage GoPage = new A.HTMlServer.HTML.GetOnePage();

        private void BForm_Load(object sender, EventArgs e)
        {
            
        }
        private void Brun_Click(object sender, EventArgs e)
        {
            if (Brun.Text == "Run")
            {
                if (!int.TryParse(TBcount.Text, out GCount))
                { 
                    GCount = 250; 
                }
                
                //执行抓取链接
                Thread Th = new Thread(GetLink);
                Th.Start();

                Thread Tu = new Thread(ThreadUi);
                Tu.Start();
            }
            else
            {
                //执行内容
            }
            Brun.Enabled = false;
        }

        #region UI
        private void ThreadUi()
        {
            do
            {
                this.Invoke(new dfun(Go));
                Thread.Sleep(1000);
            } while (true);
        }

        private void Go()
        {
            if (GoPage.Sum > GoPage.J)
            {
                pBar.Maximum = GoPage.Sum;
                pBar.Value = GoPage.J;
            }
            Lmessage.Text = "下载完成" + GoPage.J + "/" + GoPage.Sum + "个,错误"+GoPage.wrong+"个";

        }
        #endregion


        private void GetLink()
        {
            var str = "http://www.dgold.cn/plus/view.php?aid=";
            //同样的类型号的方法，可以委托多个事件
            A.HTMlServer.HThread.ToThread.Tdelegate = Hii;
            A.HTMlServer.HThread.ToThread.Tdelegate += GoPage.GetHiiGrilPageHtml;
            for (var i = 0; i < 150;i++ )
            {
                AddItemHTML(str+i);
                Thread.Sleep(500);
            }
            
        }

        /// <summary>
        /// 获取所有文章链接
        /// </summary>
        /// <param name="Html"></param>
        private void AddItemHTML(string Html)
        {
            GoPage.Sum++;
            A.HTMlServer.HThread.ToThread.AddItem(Html);
        }

        private void Hii(string Html)
        {
            
        }
    }
}
